home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / brklyprl.lha / Emulator / Benchmarks / output / pri2.pl < prev    next >
Encoding:
Text File  |  1989-04-14  |  541 b   |  23 lines

  1. % The sieve of Eratosthenes, from Clocksin & Mellish (pri2)
  2. %    finding the prime numbers up to 98.    
  3.  
  4. main :- primes(98, X), write(X), nl.
  5.  
  6. primes(Limit, Ps) :- integers(2, Limit, Is), sift(Is, Ps).
  7.  
  8. integers(Low, High, [Low | Rest]) :- 
  9.     Low =< High, !,
  10.     M is Low+1,
  11.     integers(M, High, Rest).
  12. integers(_,_,[]).
  13.  
  14. sift([],[]).
  15. sift([I | Is], [I | Ps]) :- remove(I,Is,New), sift(New, Ps).
  16.  
  17. remove(P,[],[]).
  18. remove(P,[I | Is], Nis) :- 0 is I mod P, !, remove(P,Is,Nis).
  19. remove(P,[I | Is], [I | Nis]) :- not(0 is I mod P), !, remove(P,Is,Nis).
  20.  
  21.  
  22.  
  23.